package com.web.action;

import com.util.BaseAction;
import com.web.domain.Notice;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Scope("prototype")
@ParentPackage(value = "struts-default")
@Namespace(value = "/")
@Action(value = "newsAction", results = {
        @Result(name = "detailNews", location = "/news/showNews.jsp"),
        @Result(name = "newsList", location = "/news/newsList.jsp"),
        @Result(name = "no", location = "/search/room.jsp")
})
public class NewsAction extends BaseAction {

    @Resource
    SessionFactory factory;

    private Notice notice;
    private List<Notice> noticelist;

    public List<Notice> getNoticelist() {
        return noticelist;
    }

    public void setNoticelist(List<Notice> noticelist) {
        this.noticelist = noticelist;
    }

    public Notice getNotice() {
        return notice;
    }

    public void setNotice(Notice notice) {
        this.notice = notice;
    }

    public String showNews() {
        String noticeNum;
        int realNoticeNum;
        noticeNum = request.getParameter("noticeNum");
        if (noticeNum != null){
            realNoticeNum = Integer.parseInt(noticeNum);
        }else{
            realNoticeNum = 0;
        }
        System.out.println("realNoticeNum: "+realNoticeNum);
        String hql = "from Notice t where showState = 2";
        List<Notice> notices =  factory.getCurrentSession().createQuery(hql.toString()).list();
        if(notices.size() > 0){
            for(Notice notice:notices){
                if (notice.getNoticeId() == realNoticeNum){
                    session.setAttribute("notice", notice);
                }
            }
        }
        return "detailNews";
    }

    public String newsList(){
        String hql = "from Notice t where showState = 2";
        noticelist =  factory.getCurrentSession().createQuery(hql.toString()).list();
        session.setAttribute("noticelist", noticelist);
        return "newsList";
    }


    public String execute() throws Exception {
        return null;
    }

}
